package simtester.server

import grails.test.mixin.*
import groovy.time.TimeCategory
import java.text.SimpleDateFormat
import org.junit.Before
import org.junit.Test
import simtester.server.TestRun

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(TestRun)
class TestRunTests {
    def testuser
    def testcase
    
    @Before
    void setUp() {
        testuser = new User(username: 'testuser', password: 'pass')
        testcase = new TestCase(name: 'MyTestCase', script: 'my script', owner: testuser)
    }

    @Test
    void createTestRunSuccessfully() {
       def tr = new TestRun(status: 'running', testcase: testcase)
       assert tr.validate()
    }
    
    @Test
    void createTestRunWithoutRequiredFields() {
        def tr = new TestRun()
        assert !tr.validate()
        assert 'nullable' in tr.errors['status'].codes
    }
    
    @Test
    void getDuration() {
        def starttime = new SimpleDateFormat('yyyy/MM/dd HH:mm:ss').parse('1984/10/24 04:30:30')
        def endtime = new SimpleDateFormat('yyyy/MM/dd HH:mm:ss').parse('1984/10/26 05:35:35')
        
        def duration
        use(TimeCategory) {
            duration = endtime - starttime
        }

        def tr = new TestRun(status: 'running', testcase: testcase, dateCreated: starttime, endDate: endtime)
        
        assert tr.duration.toString() == duration.toString()
    }
}
